package org.example;

/**
 * 排序链表
 */
public class Solution33 {

    public ListNode sortList(ListNode head) {
        if (head == null && head.next == null) {
            return head;
        }

        // 找到链表的中间节点
        ListNode mid = getMid(head);
        ListNode left = head;
        ListNode right = mid.next;
        mid.next = null; // 断开链表

        left = sortList(left);
        right = sortList(right);

        // 合并两个有序链表
        return merge(left, right);
    }

    /**
     * 寻找链表的中间节点
     */
    private ListNode getMid(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    private ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }

        if (l1 != null) {
            current.next = l1;
        } else {
            current.next = l2;
        }

        return dummy.next;
    }

}
